package com.iot08.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.bson.types.ObjectId;

import java.util.*;

/**
 * @version 1.0
 * @Description: 容器相关处理工具
 * @time: 2020/12/18 16:29
 * @author: Rock
 */
public class CollectionUtil {

    /**
     * long类型列表转String类型列表
     *
     * @param list
     * @time: 2020/10/31 9:44
     * @author: Rock
     * @version 1.0
     */
    public static List<String> longToStr(List<Long> list) {
        if (list != null) {
            List<String> result = new ArrayList<>();
            for (Long value : list) {
                if (value != null) {
                    result.add(String.valueOf(value));
                }
            }
            return result;
        }
        return null;
    }

    /**
     * 字符串列表转ObjectId对象列表
     *
     * @param values
     * @time: 2020/11/9 15:47
     * @author: Rock
     * @version 1.0
     */
    public static Set<ObjectId> stringToObjectId(Collection<String> values) {
        if (values != null) {
            Set<ObjectId> result = new HashSet<>();
            for (String value : values) {
                if (value != null && ObjectId.isValid(value)) {
                    result.add(new ObjectId(value));
                }
            }
            return result;
        }
        return null;
    }

    /**
     * @param list
     * @param key
     * @param type 1升序；2降序
     * @time: 2020/12/2 12:06
     * @author: Rock
     * @version 1.0
     */
    public static void sortDouble(List<Map<String, Object>> list, String key, int type) {
        Map<Double, List<Map<String, Object>>> temp = new HashMap<>();
        for (Map<String, Object> map : list) {
            if (map.get(key) instanceof Double) {
                if (!temp.containsKey((Double) map.get(key))) {
                    temp.put((Double) map.get(key), new ArrayList<>());
                }
                temp.get((Double) map.get(key)).add(map);
            }
        }
        List<Double> tempList = new ArrayList<>(temp.keySet());
        //sort后tempList为升序
        Collections.sort(tempList);
        if (type == 1) {
            //倒叙
            Collections.reverse(tempList);
        }
        for (Double d : tempList) {
            //先移除再重新添加在最前面位置
            list.removeAll(temp.get(d));
            list.addAll(0, temp.get(d));
        }
        /*Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                if(o1.get(key) instanceof Double && o2.get(key) instanceof Double){
                    if(type == 1){
                        if((Double)o1.get(key) - (Double)o2.get(key) >= 0){
                            return 0;
                        }
                    } else if(type == 2){
                        if((Double)o1.get(key) - (Double)o2.get(key) <= 0){
                            return 0;
                        }
                    }
                }
                return -1;
            }
        });*/
    }


    /**
     * @description 根据整型数据进行排序
     * @author cwj
     * @param [list, key, type]
     * @return void
     * @date @time 2025/5/21 17:38
     **/
    public static void sortInteger(List<Map<String, Object>> list, String key, int type) {
        Map<Integer, List<Map<String, Object>>> temp = new HashMap<>();
        for (Map<String, Object> map : list) {
            if (map.get(key) instanceof Integer) {
                if (!temp.containsKey((Integer) map.get(key))) {
                    temp.put((Integer) map.get(key), new ArrayList<>());
                }
                temp.get((Integer) map.get(key)).add(map);
            }
        }
        List<Integer> tempList = new ArrayList<>(temp.keySet());
        //sort后tempList为升序
        Collections.sort(tempList);
        if (type == 1) {
            //倒叙
            Collections.reverse(tempList);
        }
        for (Integer d : tempList) {
            //先移除再重新添加在最前面位置
            list.removeAll(temp.get(d));
            list.addAll(0, temp.get(d));
        }
    }

    /**
     * @description 按指定顺序排序
     * @author cwj
     * @param [list, key, type]
     * @return void
     * @date @time 2025/6/15 10:08
     **/
    public static void sortByKeyType(List<Map<String, Object>> list, String key, int type) {
        Map<Integer, List<Map<String, Object>>> temp = new HashMap<>();
        for (Map<String, Object> map : list) {
            if (map.get(key) instanceof Integer) {
                if (!temp.containsKey((Integer) map.get(key))) {
                    temp.put((Integer) map.get(key), new ArrayList<>());
                }
                temp.get((Integer) map.get(key)).add(map);
            }
        }
        List<Integer> tempList = new ArrayList<>(temp.keySet());
        //sort后tempList为升序
        Collections.sort(tempList);
        if (type == 1) {
            //倒叙
            Collections.reverse(tempList);
        }
        list.clear();
        for (Integer k : tempList) {
            list.addAll(temp.get(k));
        }
    }

    /***
    * @Description: 添加 合并 去重
    * @Param: []
    * @return:
    * @Author: Weichao
    * @Date: 2024/3/19
    */
    public static List<JSONObject> addMergeRemoveWeight(List<Map<String, Object>> list1, List<Map<String, Object>> list2){
        return new ArrayList<>();
    }

    /**
     * 删除成功返回true
     *
     * @param map
     * @param key
     * @time: 2020/12/3 9:14
     * @author: Rock
     * @version 1.0
     */
    public static boolean removeMapKey(Map<String, Map<String, Object>> map, String key) {
        if (map != null && map.containsKey(key)) {
            map.remove(key);
            return true;
        }
        return false;
    }

    /**
     * @param map
     * @param keys
     * @time: 2021/3/10 12:06
     * @author: Rock
     * @version 1.0
     */
    public static void removeMapKeys(Map<String, Map<String, Object>> map, Set<String> keys) {
        if (map != null) {
            for (String key : keys) {
                map.remove(key);
            }
        }
    }

    /**
     * 删除成功后返回被删除map
     *
     * @param map
     * @param key
     * @time: 2020/12/3 14:37
     * @author: Rock
     * @version 1.0
     */
    public static Map<String, Object> removeMapKeyData(Map<String, Map<String, Object>> map, String key) {
        if (map != null && map.containsKey(key)) {
            Map<String, Object> data = map.get(key);
            map.remove(key);
            return data;
        }
        return null;
    }

    /**
     * 判断容器是否为空
     *
     * @param map
     * @time: 2020/12/18 16:27
     * @author: Rock
     * @version 1.0
     */
    public static boolean isNotEmpty(Map<String, Map<String, Object>> map) {
        if (map == null || map.size() <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 判断容器内是否包含key
     *
     * @param map
     * @param key
     * @time: 2021/1/8 9:27
     * @author: Rock
     * @version 1.0
     */
    public static boolean containsKey(Map<String, Map<String, Object>> map, String key) {
        if (map == null || map.size() <= 0) {
            return false;
        }
        return map.containsKey(key);
    }


}
